using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SmartContentUI.Util
{
    public partial class OptionPane : Form
    {
        #region Enum/Constants
        public enum InputType
        {
            Numeric,
            String,
            DateTime
        }
        #endregion
        #region Variables

        private static OptionPane that = null;
        private Regex reg;
        private InputType type;
        private Object inputCtrl;
        private string data;
        #endregion
        private OptionPane()
        {
            InitializeComponent();
        }

        public void setCaption(string text)
        {
            this.captionLbl.Text = text;
        }

        public void setMessage(string text)
        {
            this.msgLbl.Text = text;
        }

        public void setInputType(InputType type)
        {
            switch (type)
            {
                case InputType.Numeric:
                    TextBox inputN = new TextBox();
                    inputN.Size = new Size(175, 20);
                    inputN.Anchor = AnchorStyles.None;
                    this.tableLayoutPanel1.Controls.Add(inputN, 0, 2);
                    inputCtrl = inputN;
                    inputN.Focus();
                    reg = new Regex(@"\d+(\.\d+)?");
                    this.type = type;
                    break;
                case InputType.String:
                    TextBox inputS = new TextBox();
                    inputS.Size = new Size(175, 20);
                    inputS.Anchor = AnchorStyles.None;
                    this.tableLayoutPanel1.Controls.Add(inputS, 0, 2);
                    inputCtrl = inputS;
                    inputS.Focus();
                    this.type = type;
                    break;
                case InputType.DateTime:
                    DateTimePicker inputD = new DateTimePicker();
                    inputD.Size = new Size(175, 20);
                    inputD.Anchor = AnchorStyles.None;
                    this.tableLayoutPanel1.Controls.Add(inputD, 0, 2);
                    inputCtrl = inputD;
                    inputD.Focus();
                    this.type = type;
                    break;
            }
        }
        public string getData()
        {
            return data;
        }

        public static string InputMessage(string caption, string message, InputType inputType)
        {
            that = new OptionPane();
            that.setCaption(caption);
            that.setMessage(message);
            that.setInputType(inputType);
            that.ShowDialog();
            return that.getData();
        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            switch (type)
            {
                case InputType.Numeric:
                    if (!reg.IsMatch(((TextBox)inputCtrl).Text))
                    {
                        string tmp = ((TextBox)inputCtrl).Text;
                        ((TextBox)inputCtrl).Text = "Debe digitar un numero";
                        this.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        ((TextBox)inputCtrl).Text = tmp;
                    }
                    else
                    {
                        data = ((TextBox)inputCtrl).Text;
                        this.Close();
                    }
                    break;
                case InputType.String:
                    data = ((TextBox)inputCtrl).Text;
                    this.Close();
                    break;
                case InputType.DateTime:
                    data = ((DateTimePicker)inputCtrl).Text;
                    this.Close();
                    break;
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.data = null;
            this.Close();
        }
    }
}